#!/usr/bin/env python
# encoding: utf-8
# @Time : 2020/4/23 14:39 
# @Author : 能量咖啡豆 
# @File : 83.py 
# @desc : 83. 删除排序链表中的重复元素

class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

"""
扫描链表，重新构建，但该方案在leetcode提交不过
"""
def solution(head):
    index = head
    vals = [head.val]
    while index != None:
        if index.val != vals[-1]:
            vals.append(index.val)
        else:
            index = index.next
    head0 = None
    index0 = None
    for i in range(len(vals)):
        if index0 == None:
            index0 = ListNode(vals[i])
            head0 = index0
        else:
            tempnode = ListNode(vals[i])
            index0.next = tempnode
            index0 = index0.next

    return head0

"""
双指针扫描解法
"""
def solution2(head):
    findex = head
    lindex = findex.next
    while lindex != None:
        if findex.val == lindex.val:
            if lindex.next == None:
                findex.next = None
            lindex = lindex.next
        else:
            findex.next = lindex
            findex = lindex
            lindex = lindex.next
    return head


if __name__ == "__main__":
    print("hello")
    n1 = ListNode(1)
    n2 = ListNode(2)
    n3 = ListNode(3)
    n4 = ListNode(3)
    n5 = ListNode(4)
    #n1.next = n2
    #n2.next = n3
    #n3.next = n4
    #n4.next = n5
    f = solution2(n1)
    while f != None:
        print(f.val)
        f = f.next
